/**
 * https://leetcode.cn/submissions/detail/583934591/
 * 429. N 叉树的层序遍历
 * Medium, 黄伟杰 2024.11.29
 * BFS
 */

class Solution
{
public:
    vector<vector<int>> levelOrder(Node *root)
    {
        if (root == nullptr)
            return {};
        vector<vector<int>> ans;
        queue<Node *> q;
        q.push(root);
        while (!q.empty())
        {
            vector<int> cnt;
            for (int i = q.size(); i > 0; i--) // 提取孩子次数
            {
                auto p = q.front();
                q.pop();
                cnt.push_back(p->val);
                for (auto j : p->children)
                    q.push(j);
            }
            ans.push_back(cnt);
        }
        return ans;
    }
};
